package model

import business.*
import enums.Direction
import org.itheima.kotlin.game.core.Painter
import java.util.*

class Tank(i: Int,j: Int) :Moveable,Blockable,Sufferable,Destroyable{
    override fun isDestroy(): Boolean {
        if(blood<0){
            return true
        }
        return false
    }

    override var blood: Int=10

    override fun notifySuffer(attack: Attackable): Array<IView>? {
        if(attack.owner==this){
            return null
        }
        blood-=attack.attackPower
        return arrayOf(Blast(x,y))
    }

    override var width: Int=Config.block
    override var height: Int=Config.block
    override var x: Int=width*j
    override var y: Int=height*i
    override var badDirection: Direction?=null
    override var direction = Direction.UP
    override val speed:Int=Config.block/2//移动速度
    var bullet:Bullet?=null//子弹对象
    override fun draw() {
        val img_path=when(direction){
            Direction.UP -> "img/tank_u.gif"
            Direction.DOWN -> "img/tank_d.gif"
            Direction.LEFT -> "img/tank_l.gif"
            Direction.RIGHT -> "img/tank_r.gif"
            else -> throw Exception("坦克方向异常")
        }
        Painter.drawImage(img_path,x,y)
    }

    var lastMoveTime=0L
    var currMoveTime=0L
    var moveFrequency=50
    //坦克移动
    fun move(dir:Direction){
        currMoveTime=System.currentTimeMillis()
        if(currMoveTime-lastMoveTime<=moveFrequency){
            return
        }
        lastMoveTime=currMoveTime
        if(direction!=dir){
            direction=dir
            return
        }
        //println("badDirection:$badDirection")
        if(dir==badDirection){
            return
        }
        direction=dir
        when(dir){
            Direction.UP -> y-=speed
            Direction.DOWN -> y+=speed
            Direction.LEFT -> x-=speed
            Direction.RIGHT -> x+=speed
        }
        if(y<0) y=0
        if(x<0) x=0
        if(x>Config.gameWidth-this.width) x=Config.gameWidth-this.width
        if(y>Config.gameHeight-this.height) y=Config.gameHeight-this.height

    }

    var lastFireTime=0L
    var currFireTime=0L
    var fireFrequency=800
    /**
     * 发射子弹
     */
    fun fire(){
        currFireTime=System.currentTimeMillis()
        if(currFireTime-lastFireTime<=fireFrequency){
            bullet=null
            return
        }
        lastFireTime=currFireTime
        bullet=Bullet(this,direction, {
            w,h->
                var xx:Int=0
                var yy:Int=0
                when(direction){
                    Direction.UP -> {
                        xx=this.x+this.width/2-w/2
                        yy=this.y-h/2
                    }
                    Direction.DOWN -> {
                        xx=this.x+this.width/2-w/2
                        yy=this.y+this.height-h/2
                    }
                    Direction.LEFT -> {
                        xx=this.x-w/2
                        yy=this.y+this.height/2-h/2
                    }
                    Direction.RIGHT -> {
                        xx=this.x+this.width-w/2
                        yy=this.y+this.height/2-h/2
                    }
                }
                Pair<Int,Int>(xx,yy)
        })
    }

}